package com.wpen.unit.utils;

import java.util.HashMap;
import java.util.Map;

public class ThreadLocalUtil {
	private static ThreadLocal<Map<String, Object>> theadLocal = new ThreadLocal<>();

	public static void set(String key, Object value) {
		if (theadLocal.get() == null) {
			theadLocal.set(new HashMap<String, Object>());
		}
		((Map<String, Object>) theadLocal.get()).put(key, value);
	}

	public static Object get(String key) throws Exception {
		if (theadLocal.get() == null) {
			return null;
		}
		return ((Map<String, Object>) theadLocal.get()).get(key);
	}

	public static void remove(String key) {
		if (theadLocal.get() != null) {
			((Map<String, Object>) theadLocal.get()).remove(key);
			if (((Map<String, Object>) theadLocal.get()).isEmpty())
				theadLocal.remove();
		}
	}

	public static void removeAll() {
		theadLocal.remove();
	}
}